package org.firebug.spring.boot.hikari.service;

import org.firebug.spring.boot.hikari.controller.dto.UserDto;
import org.firebug.spring.boot.hikari.repository.dao.UserDao;
import org.firebug.spring.boot.hikari.repository.po.UserPo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public void save(UserDto dto) {
        UserPo po = new UserPo();
        BeanUtils.copyProperties(dto, po);
        userDao.save(po);
    }

    public void removeById(Long id) {
        userDao.deleteById(id);
    }

    public void modifyById(UserDto dto) {
        UserPo po = userDao.queryById(dto.getId());
        if (po == null) {
            throw new RuntimeException("t_user(id=" + dto.getId() + ") not exist");
        }
        BeanUtils.copyProperties(dto, po);
        userDao.save(po);
    }

    public UserDto queryById(Long id) {
        UserPo po = userDao.queryById(id);
        if (po == null) {
            throw new RuntimeException("t_user(id=" + id + ") not exist");
        }
        UserDto dto = new UserDto();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }
}
